package org.example;

import java.util.Arrays;

/**
 * 2279. 装满石头的背包的最大数量
 */
public class MaximumBags {
    public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
        int length = capacity.length;
        int[] remain = new int[length];
        for (int i = 0; i < length; i++) {
            remain[i] = capacity[i] - rocks[i];
        }
        Arrays.sort(remain);
//        for (int i= 0; i< length-1;i++ ) {
//            for(int j= i+1; j< length; j++) {
//                if(remain[j]< remain[i]) {
//                    int temp = remain[j];
//                    remain[j] = remain[i];
//                    remain[i] = temp;
//                }
//            }
//        }
        int result = 0;
        for (int i = 0; i < length; i++) {
            additionalRocks = additionalRocks - remain[i];
            if(additionalRocks >= 0 ) {
                result++;
            } else {
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        MaximumBags stone = new MaximumBags();
        int[] capacity = {2,3,4,5};
        int[] rocks = {1,2,4,4};
        int result = stone.maximumBags(capacity, rocks, 2);
        System.out.println(result);

        int[] capacity2 = {10,2,2};
        int[] rocks2 = {2,2,0};
        int result2 = stone.maximumBags(capacity2, rocks2, 100);
        System.out.println(result2);
    }
}
